<template>
  <div clas="fee-summary">
    <div class="fee-summary-header"> 费用信息 </div>
    <fee-statistic-content :feeForm="feeForm" />
  </div>
</template>
<script setup>
  import { inject, computed } from 'vue'
  import feeStatisticContent from './fee-statistic-content.vue'
  import { initFeeDetail, handleStoreTemplateDetail } from './fee-utils.js'
  import { decimalAddition, decimalSubtract } from '@/utils'
  import { cloneDeep } from 'lodash-es'

  const buildStList = (info) => {
    // 创建费用时的费用模板快照
    let priceTemplateSnapshot = info.priceTemplateSnapshot
    // 解析后的费用模板
    let storeTemplate = null
    if (priceTemplateSnapshot) {
      priceTemplateSnapshot = JSON.parse(priceTemplateSnapshot)
      storeTemplate = handleStoreTemplateDetail(priceTemplateSnapshot)
    }
    let temp = {
      orderFee: [
        {
          // 费用类型（线上费用|线下费用）
          feeType: info.feeType,
          feeDetailList: [],
          feeDetailTypeList: ['basicFee', 'addServiceFee', 'preferentialFee', 'otherFee'],
        },
      ],
    }
    initFeeDetail(temp, storeTemplate)
    // 回显
    // 默认费用
    let feeDetailList = info.feeDetailList
    let formfeeDetailList = temp.orderFee[0].feeDetailList
    formfeeDetailList.forEach((formFeeDetailItem) => {
      let findResult = feeDetailList.find((item) => {
        let sameFeeCategory = item.feeCategory == formFeeDetailItem.feeCategory
        let sameFeeName = item.itemName == formFeeDetailItem.feeName
        return sameFeeCategory || sameFeeName
      })
      if (findResult) {
        formFeeDetailItem.categoryType = findResult.type
        formFeeDetailItem.id = findResult.id
      } else {
        formFeeDetailItem.categoryType = 0
      }
      formFeeDetailItem.childrenList?.forEach((item) => {
        let findFeeDetail = findResult?.childrenList.find((feeDetailItem) => feeDetailItem.itemName == item.itemName)
        if (!findFeeDetail) {
          findFeeDetail = feeDetailList.find((feeDetailItem) => feeDetailItem.itemName == item.itemName)
        }
        if (findFeeDetail) {
          item.amount = findFeeDetail.amount
          item.id = findFeeDetail.id
          item.statics = true
          item.costType = findFeeDetail.costType
        }
      })
    })

    return temp.orderFee[0]
  }

  // 当前 feeForm 中含有未解构的原始数据，需要对其进行处理
  const resFeeForm = inject('feeForm')
  const feeForm = computed(() => {
    let orderFee = cloneDeep(resFeeForm.value.orderFee)
    orderFee.forEach((item, index) => {
      if (item.id > 0) {
        orderFee[index] = buildStList(item)
      }
    })
    let tempFeeForm = {
      feeType: 1,
      feeDetailList: [
        { feeCategory: 0, statics: true, feeName: '基础费用', childrenList: [] },
        { feeCategory: 1, statics: true, feeName: '增值服务费', childrenList: [] },
        { feeCategory: 2, statics: true, feeName: '优惠费用', childrenList: [] },
        { feeCategory: 3, statics: true, feeName: '其他费用', childrenList: [] },
      ],
    }
    // 数据合并
    orderFee.forEach((item) => {
      const feeDetailList = item.feeDetailList
      feeDetailList.forEach((feeItem) => {
        const { feeCategory, childrenList } = feeItem
        const target = tempFeeForm.feeDetailList.find((tempItem) => tempItem.feeCategory == feeCategory)
        const tartgetChildrenList = target?.childrenList
        if (tartgetChildrenList) {
          childrenList.forEach((childItem) => {
            if (childItem.amount) {
              childItem.statics = true
              let childTarget = tartgetChildrenList.find((tarItem) => tarItem.itemName == childItem.itemName)
              if (childTarget) {
                let sum = childTarget.amount || 0
                sum = decimalAddition(sum, childItem.amount)
                childTarget.amount = sum
                childTarget.statics = true
              } else {
                tartgetChildrenList.push(childItem)
              }
            }
          })
        }
      })
    })
    let temp = {
      orderFee: [tempFeeForm],
    }
    return temp
  })
</script>
<style lang="less" scoped>
  .fee-summary-header {
    height: 40px;
    line-height: 40px;
    padding: 0 12px;
    background: #f2f3f5;
    font-weight: 600;
    color: #1d2129;
  }
</style>
